<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 8/24/14
 * Time: 3:54 PM
 */

class Gallery extends MY_Home {
    public function item($id = null) {
        $cartDetails = $this->getCartDetails();
        $clientName = $this->isClientLoggedIn();
        $categories = $this->findAll("Category");
        $terms = $this->findContentByType(Contents::CONTENT_TERMS_AND_CONDITION);
        foreach($categories as $category){
            if($category->getParent() == null){
                $noOfSub = 0;
                foreach($categories as $sub){
                    if($sub->getParent() != null && $sub->getParent()->getId() == $category->getId()){
                        $noOfSub++;
                    }
                }
                $category->setNoOfSub($noOfSub);
            }
        }
        $selectedCategory = "All Categories";
        foreach ($categories as $category) {
            if ($id == $category->getId()) {
                $selectedCategory = $category->getName();
            }
        }
        $data = array("cart"=>$cartDetails,"user" => $clientName, "categories" => $categories, "selectedCategory" => $selectedCategory, "selectedCategoryId" => $id,"terms"=>$terms);
        $this->load->view('gallery', $data);
    }

    public function details($id = null) {
        $cartDetails = $this->getCartDetails();
        $clientName = $this->isClientLoggedIn();
        $categories = $this->findAll("Category");
        $terms = $this->findContentByType(Contents::CONTENT_TERMS_AND_CONDITION);
        foreach($categories as $category){
            if($category->getParent() == null){
                $noOfSub = 0;
                foreach($categories as $sub){
                    if($sub->getParent() != null && $sub->getParent()->getId() == $category->getId()){
                        $noOfSub++;
                    }
                }
                $category->setNoOfSub($noOfSub);
            }
        }
        $locations = $this->findAll("Branch");
        $product = null;
        $relateProducts = null;
        if ($id != null) {
            $product = $this->findById("Product", $id);
            $relatedCategory = $product->getCategory();
            $query = $this->em->createQuery("SELECT o FROM Product o WHERE o.category = :category AND o.active = true ORDER BY o.dateCreate DESC");
            $query->setMaxResults(12);
            $query->setParameter("category", $relatedCategory);
            $relateProducts = $query->getResult();
        }
        $data = array("cart"=>$cartDetails ,"user" => $clientName, "categories" => $categories, "locations" => $locations, "product" => $product, "relateProducts" => $relateProducts,"terms"=>$terms);
        $this->load->view('productDetails', $data);
    }

    public function getProductByCategory($id = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            if ($id != null) {
                $category = $this->findById("Category", $id);
                $query = $this->em->createQuery("SELECT o FROM Product o WHERE o.category = :category AND o.active = true ORDER BY o.price DESC");
                $query->setParameter("category", $category);
            } else {
                $query = $this->em->createQuery("SELECT o FROM Product o where o.active = true ORDER BY o.dateCreate DESC");
            }
            $products = $query->getResult();
            foreach ($products as $product) {
                $product->setImages($product->getImages()->toArray());
                $product->setStock($product->getStock()->toArray());
            }
            $response->setData($products);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function getRecentProducts() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $query = $this->em->createQuery("SELECT o FROM Product o WHERE o.active = true ORDER BY o.dateCreate DESC");
            $query->setMaxResults(12);
            $products = $query->getResult();
            foreach ($products as $product) {
                $product->setImages($product->getImages()->toArray());
                $product->setStock($product->getStock()->toArray());
            }
            $response->setData($products);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function getProductStock($id = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            if ($id == null) {
                throw new RuntimeException("Invalid Product Id");
            }
            $product = $this->findById("Product", $id);
            $response->setData($product->getStock()->toArray());
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }

    public function search(){
        $cartDetails = $this->getCartDetails();
        $clientName = $this->isClientLoggedIn();
        $categories = $this->findAll("Category");
        $terms = $this->findContentByType(Contents::CONTENT_TERMS_AND_CONDITION);
        foreach($categories as $category){
            if($category->getParent() == null){
                $noOfSub = 0;
                foreach($categories as $sub){
                    if($sub->getParent() != null && $sub->getParent()->getId() == $category->getId()){
                        $noOfSub++;
                    }
                }
                $category->setNoOfSub($noOfSub);
            }
        }
        $selectedCategory = "All Categories";
        $productName = $this->input->post("product-name");
        $data = array("productName"=> $productName,"cart"=>$cartDetails,"user" => $clientName, "categories" => $categories, "selectedCategory" => $selectedCategory, "terms"=>$terms);
        $this->load->view('search', $data);
    }

    public function searchProductByName(){
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $productName = strip_tags(trim($this->input->post("product-name")));
        $response = new Response();
        try {
            $query = $this->em->createQuery("SELECT o FROM Product o WHERE o.name LIKE :name AND o.active = true");
            $query->setParameter("name", "%" . $productName . "%");
            $products = $query->getResult();
            if ($products == null || count($products) == 0) {
                throw new RuntimeException("No Product was found by name :" . $productName);
            }
            foreach ($products as $product) {
                $product->setImages($product->getImages()->toArray());
                $product->setStock($product->getStock()->toArray());
            }
            $response->setData($products);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response, JSON_PRETTY_PRINT);
    }
} 